package com.bruce.learnrecycler.kotlin.action

import android.os.Parcel
import android.os.Parcelable
import com.bruce.learnrecycler.kotlin.ui.listitem.Item
import com.bruce.learnrecylerview.app.R
import com.bruce.learnrecylerview.app.action.MainActions
import com.bruce.learnrecylerview.app.data.WebApi
import com.bruce.learnrecylerview.app.model.shjt.carmonitor.Result
import com.bruce.learnrecylerview.app.model.shjt.getline.LineInfoDetail
import com.github.bluzwang.fluxsupport
import com.github.bluzwang.fluxsupportkotlin.Dispatcher
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.io.Serializable
import java.util.ArrayList
import javax.inject.Inject

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/2.
 */
public class ActionDelegate Inject constructor(val dispatcher: Dispatcher, val api: WebApi):Serializable {


    public fun requestLineInfoDetailByName(lineName: String, isPx: Boolean): Observable<LineInfoDetail>?
            = api.requestLineInfo(lineName, isPx)
            ?.flatMap {
                it?: throw IllegalArgumentException("line info is null with lineName:${lineName}")
                api.requestLineInfoDetail(it.getLine_id(), isPx)
            }

    data class DataHolder(val stopId: String, val lineId: String, val isPX: Boolean)

    public fun findOutStationInDetail(detail: LineInfoDetail, stationName: String): List<DataHolder> {
        var stops = ArrayList<DataHolder>();
        var stop1 = detail.getStops0().firstOrNull() { it.getZdmc() equals stationName }
        var stop2 = detail.getStops1().firstOrNull() { it.getZdmc() equals stationName }
        if (stop1 != null) {
            stops add DataHolder(stop1.getId(), detail.lineId, detail.getDirection0())
        }
        if (stop2 != null) {
            stops add DataHolder(stop2.getId(), detail.lineId, detail.getDirection1())
        }
        return stops
    }

    public fun requestStationByLineNameAndStationName(lineName: String, stationName: String, isPX: Boolean): Observable<List<DataHolder>>?
            = requestLineInfoDetailByName(lineName, isPX)
            ?.map {
                it?: throw IllegalArgumentException("line info detail is null with lineName:${lineName}, stationName:${stationName}")
                findOutStationInDetail(it, stationName)
            }


    public fun requestResultByLineNameAndStationName(lineName: String, stationName: String, isPx: Boolean): Observable<Result>?
            = requestStationByLineNameAndStationName(lineName, stationName, isPx)
            ?.flatMap { Observable.from(it) }
            ?.flatMap { api.requestResult(it.lineId, it.isPX, it.stopId, isPx) }



    fun requestByLineNameStationName(owner:Int,lineName: String, stationName: String, isPx: Boolean, index: Int=-2) {
        requestResultByLineNameAndStationName(lineName, stationName, isPx)
                ?.filter { it.getCars().size() > 0 }
                ?.map { Item((it.getCars() get 0).getTerminal(), R.layout.base_item) }
                ?.compose { it subscribeOn Schedulers.io() observeOn AndroidSchedulers.mainThread() }
                ?.subscribe ({
                    (dispatcher dispatchRequestToStoreWithType MainActions.SHOW_PD_BUS
                            key MainActions.KEY_RESULT_ITEM toValue it!!
                            key MainActions.KEY_ITEM_INDEX toValue index
                            from owner post null )
                }) {
                    it.printStackTrace()
                }
    }
}
